package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.business.interfaces.orderstock.OrderStockBService;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.model.dto.orderstock.ReleaseStockDTO;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.business.interfaces.work.customwork.ZsDistributeWorkBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName OmsOrderReleaseStockListener
 * @description:
 * @author: wangyanci
 * @create: 2021-12-15 18:54
 * @Version 1.0
 **/
@Slf4j
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
public class OmsOrderReleaseStockListener {

    @Autowired
    private ZsDistributeWorkBService zsDistributeWorkBService;
    @Autowired
    OrderWorkService orderWorkService;
    @Autowired
    OrderStockBService orderStockBService;


    @AliyunMQHandler(tags = OmsMqTagConsts.ORDER_MASTER_RELEASE_STOCK)
    public void releaseStock(String message){
        log.info("OmsOrderReleaseStockListener message={}", message);
        try {
            ReleaseStockDTO releaseStockDTO = JSON.parseObject(message, ReleaseStockDTO.class);
            OrderWork orderWork = orderWorkService.findOrderWorkByKey(releaseStockDTO.getOrderId(), releaseStockDTO.getWorkId());
            boolean waitAutoDistribute = zsDistributeWorkBService.isWaitAutoDistribute(orderWork);
            log.info("waitAutoDistribute={}", waitAutoDistribute);
            if (!waitAutoDistribute) {
                return;
            }
            if (orderWork.getStatus() < OrderStatusConsts.WORK_STATUS_DISTRIBUTE) {
                orderStockBService.releaseStock(releaseStockDTO);
            }
        } catch (Exception e) {
            log.error("OmsOrderReleaseStockListener error={}", e.getMessage());
        }
    }

}